package com.ebt.m.customer.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.constant.CustomerConstant;
import com.ebt.m.customer.event.EventCustomerFloatingButtonAction;
import com.ebt.m.customer.event.EventEmpty;
import com.ebt.m.fragment.BaseFragment;
import com.ebt.m.event.EventRefreshProposalList;
import com.ebt.m.share.ShareEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 客户详情-计划书
 */
public class FragmentCustomerPropose extends BaseFragment {

    private Bundle mGetBundle;
    private String mCustomerUuid;
    private String mCustomerName;

    @BindView(R.id.fragment_customer_propose_list_view)
    CustomerProposeListView mListView;
    @BindView(R.id.iv_empty)
    ImageView mEmptyImageView;
    @BindView(R.id.tv_empty)
    TextView mEmptyTextView;
    @BindView(R.id.btn_empty)
    Button mEmptyButton;
    @BindView(R.id.empty_container)
    LinearLayout mEmptyView;
    Unbinder unbinder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initArgu();
        EventBus.getDefault().register(this);
    }


    @Override
    public void onCreateView(Bundle savedInstanceState) {
        setContentView(R.layout.fragment_customer_propose);
        unbinder = ButterKnife.bind(this, contentView);
        mEmptyButton.setVisibility(View.VISIBLE);
        mEmptyButton.setText("新建计划书");
        contentView.postDelayed(new Runnable() {
            @Override
            public void run() {
                initValues();
            }
        }, CustomerConstant.Delay.FRAGMENT_UPDATE_DELAY);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }

    @Subscribe
    public void onEvent(EventRefreshProposalList event) {
        //刷新显示计划书列表
        updateData();
    }

    /**
     * 空图和错误图片显示逻辑
     *
     * @param empty
     */
    public void showEmpty(boolean empty) {
        if (empty) {
            mEmptyView.setVisibility(View.VISIBLE);
            mEmptyTextView.setText("暂无计划书");
            mEmptyImageView.setImageResource(R.drawable.ic_empty_proposal);
            mEmptyView.setVisibility(View.VISIBLE);
        } else {
            mEmptyView.setVisibility(View.GONE);
        }
    }


    public static FragmentCustomerPropose newInstance(String customerUuid, String customerName) {
        Bundle bundle = new Bundle();
        bundle.putString(CustomerConstant.Customer.UUID, customerUuid);
        bundle.putString(CustomerConstant.Customer.NAME, customerName);
        FragmentCustomerPropose fragmentCustomerPropose = new FragmentCustomerPropose();
        fragmentCustomerPropose.setArguments(bundle);
        return fragmentCustomerPropose;
    }

    private void initArgu() {
        mGetBundle = getArguments();
        if (mGetBundle != null) {
            mCustomerUuid = mGetBundle.getString(CustomerConstant.Customer.UUID);
            mCustomerName = mGetBundle.getString(CustomerConstant.Customer.NAME);
        }
    }

    public void initValues() {
        updateData();

    }


    private void updateData() {
        if (mListView != null) {
            mListView.update(mCustomerUuid);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ShareEvent event) {
        if (event != null && event.state == event.success) {
            initValues();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventEmpty event) {
        showEmpty(event.isEmpty);
    }

    @OnClick({R.id.btn_empty})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_empty:
                EventBus.getDefault().post(new EventCustomerFloatingButtonAction(EventCustomerFloatingButtonAction.Proposal));
                break;
        }
    }
}